/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import javax.inject.Inject;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;
import session.stateless.StakeholderSessionBean;
import util.entityHelper.StakeholderAccountState;
import util.exception.ExistException;

/**
 *
 * @author jamie
 */
@ManagedBean(name = "stakeholderPortalManagedBean")
@ViewScoped
public class AddStakeholderPortalInfoManagedBean implements Serializable {

    @EJB
    private StakeholderSessionBean stakeholderPortalMgt;
    private String userName;
    private long stakeholderID;
    private String stakeholderDesc;
    private String imgUrl;
    private StakeholderAccountState stakeholderAccountState;
    //@Inject 
    private MediaUploadManagedBean uploadBean;

    public void initView(PhaseEvent event) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        userName = (String) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("stakeholderUsername");
        //stakeholderID = (long) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("stakeholderId");
        if (stakeholderAccountState == null) {
            setStakeholderAccountState((StakeholderAccountState) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("stakeholderAccountState"));
            stakeholderID = getStakeholderAccountState().getId();

            System.out.println("userName: " + userName);
            System.out.println("userId: " + stakeholderID);

            try {
                setStakeholderAccountState(stakeholderPortalMgt.getStakeholderPortalInfoById(stakeholderID));
                //  imgUrl = stakeholderAccountState.getImgUrl();
                //  stakeholderDesc = stakeholderAccountState.getStakeholderDesc();
                //  System.out.println("Stakeholder desc: " +stakeholderDesc);
            } catch (ExistException ex) {
                // EXCEPTION
                ex.printStackTrace();
            }
        }
    }

    public void saveStakeholderPortalInfo(ActionEvent event) {//throws UnsupportedEncodingException, NoSuchAlgorithmException{

        System.out.println("stakeholder saved description: " + stakeholderAccountState.getStakeholderDesc());
        boolean success = false;
        
        //System.out.println("the image path: "+ uploadBean.getImageFilePath());
        //stakeholderAccountState.setImgUrl(uploadBean.getImageFilePath()); 
       
       //userName = (String) FacesContext.getCurrentInstance().get.getSessionMap().get("stakeholderUsername"); 
       stakeholderAccountState.setImgUrl((String) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("imageFilePath"));
        
        System.out.println("what's the path of image "+imgUrl);
        //stakeholderAccountState.setImgUrl(bean.getImageFilePath());
        
        try {
            success = stakeholderPortalMgt.addPortalInfoByInfo(stakeholderAccountState);
            System.out.println("success: " + success);
        } catch (ExistException ex) {
            ex.printStackTrace();
        }
        if (success) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Stakeholder Portal information added successfully", ""));
            //this.stakeholderDesc = stakeholderDesc;
        }

    }

    /**
     * @return the userName
     */
    public String getUserName() {
        return userName;
    }

    /**
     * @return the stakeholderID
     */
    public long getStakeholderID() {
        return stakeholderID;
    }

    /**
     * @return the stakeholderDesc
     */
    public String getStakeholderDesc() {
        return stakeholderDesc;
    }

    /**
     * @return the imgUrl
     */
    public String getImgUrl() {
        return imgUrl;
    }

    /**
     * @param userName the userName to set
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }

    /**
     * @param stakeholderID the stakeholderID to set
     */
    public void setStakeholderID(long stakeholderID) {
        this.stakeholderID = stakeholderID;
    }

    /**
     * @param stakeholderDesc the stakeholderDesc to set
     */
    public void setStakeholderDesc(String stakeholderDesc) {
        this.stakeholderDesc = stakeholderDesc;
    }

    /**
     * @param imgUrl the imgUrl to set
     */
    public void setImgUrl(String imgUrl) {
        this.imgUrl = imgUrl;
    }

    /**
     * @return the stakeholderAccountState
     */
    public StakeholderAccountState getStakeholderAccountState() {
        return stakeholderAccountState;
    }

    /**
     * @param stakeholderAccountState the stakeholderAccountState to set
     */
    public void setStakeholderAccountState(StakeholderAccountState stakeholderAccountState) {
        this.stakeholderAccountState = stakeholderAccountState;
    }

    /**
     * @return the uploadBean
     */
    public MediaUploadManagedBean getUploadBean() {
        return uploadBean;
    }

    /**
     * @param uploadBean the uploadBean to set
     */
    public void setUploadBean(MediaUploadManagedBean uploadBean) {
        this.uploadBean = uploadBean;
    }
}
